<?php declare(strict_types=1);


namespace App\Model\Logic;


use App\Helper\RedisConst;
use App\Model\Entity\Members;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Redis\Pool;


/**
 * @Bean()
 * Class UUIDCheckLogic
 * @package App\Model\Logic
 */
class UUIDCheckLogic
{


    /**
     * @Inject("redis.pool")
     * @var Pool
     */
    private $redis;

    /**
     * --------------------------------------------------------------------------
     * 设置uuid member_id 映射
     * --------------------------------------------------------------------------
     * @param $uuid
     * @param $member_id
     */
    public function setMemberId($uuid, $member_id)
    {
        $this->redis->set(sha1($uuid), $member_id, RedisConst::NORMAL_EXPIRE);
    }


    /**
     *
     * 由uuid 获取member_id
     * @param $uuid
     * @return |null
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     * @throws \Swoft\Db\Exception\DbException
     */
    public function getMemberId($uuid)
    {
        if(!trim($uuid)) return null;
        $k = sha1($uuid);
        $flag = $this->redis->exists($k);
        if(!$flag) {
            $member = Members::where('uuid', $uuid)->first();
            if(!$member) return null;
            $this->setMemberId($uuid, $member->id);
            return $member->id;
        }
        return $this->redis->get($k);
    }

}